<?php

namespace app\queueHandle\controller;

//处理news 相关的异步处理
use think\Db;
use WebGeeker\Validation\Validation;

class NewsHandle
{

    //文章点赞和踩和取消赞取消踩的处理(总的点赞数处理)
    public static function treadFavourHandle($data)
    {
        //文章的id
        $item_id = $data['item_id'];
        //用户的操作
        $status = $data['status'];
        //操作之前的状态
        $result_status = $data['result_status'];

        //获取当前用户的踩的数和赞的数
        $mongo_obj = Db::connect('db_mongo')->name('item');

        //当前的月份
        $month = date('Ymd');

        //文章的数据
        $item_info = $mongo_obj->where(['id' => $item_id])->field('favour_numbers,tread_numbers,' . $month)->find();

        //0:取消点赞,1:点赞,2:踩,3:取消踩(用户的操作)
        //var_dump($status);
        //用户操作之前的状态
        //var_dump($result_status);

        switch ($status) {
            case 0:
                echo '取消点赞';
                $item_info['favour_numbers']--;
                break;
            case 1:
                echo '点赞';
                $item_info['favour_numbers']++;
                if ($result_status == 2) {

                    if ($item_info['tread_numbers'] > 0) {
                        $item_info['tread_numbers']--;
                    }
                }
                break;
            case 2:
                echo '踩';
                $item_info['tread_numbers']++;
                if ($result_status == 1) {

                    if ($item_info['favour_numbers'] > 0) {
                        $item_info['favour_numbers']--;
                    }
                }
                break;

            case 3:
                echo '取消踩';
                $item_info['tread_numbers']--;
                break;
        }

        //更新数据
        //$update_result = $mongo_obj->where(['id'=>$item_id])->update($item_info);
        //var_dump($update_result);

        //去除数组中的id
        unset($item_info['id']);

        //当前更新了文章的总的点赞数与总的踩数
        $result = $mongo_obj->where(['id' => $item_id])->update($item_info);
    }

    //文章的阅读数处理
    public static function readHandle($data)
    {

        $item_id = $data['item_id'];


        $mongo_obj = Db::connect('db_mongo')->name('item');

        //文章的
        $result = $mongo_obj->where(['id' => $item_id])->setInc('read_numbers');

    }

    //文章分享数的处理
    public static function shareHandle($data)
    {

        $item_id = $data['item_id'];

        $mongo_obj = Db::connect('db_mongo')->name('item');

        //文章总的分享数加1
        $result = $mongo_obj->where(['id' => $item_id])->setInc('share_numbers');

    }

    //计算当前文章的的热度(根据点赞数和被回复数)
    public static function newsHeatCountHandle($data)
    {

        //评论的id
        $remark_id = $data['remark_id'];

        //根据评论的id获取评论的内容
        $item_remark_obj = Db::connect('db_mongo')->name('item_remark');

        //判断当前评论是不是存在
        $item_remark_result = $item_remark_obj->where(['id' => $remark_id])->field('remark_favour_numbers,remark_by_numbers')->find();

        if ($item_remark_result) {

            $item_heat_base = Db::connect('db_mongo')->name('item_heat_base')->find();

            if ($item_heat_base) {

                $remark_heat = $item_heat_base['favour_numbers_base'] * $item_remark_result['remark_favour_numbers'] +
                    $item_heat_base['remark_numbers_base'] * $item_remark_result['remark_by_numbers'];

                //更新评论的热度
                $updateResult = Db::connect('db_mongo')->name('item_remark')
                    ->where(['id' => $remark_id])->update(['remark_heat' => $remark_heat]);

                if ($updateResult) {
                    echo "更新评论热度成功";
                } else {
                    echo "更新评论热度失败";
                }

            } else {
                exit('基数数据不存在');
            }

        } else {
            exit('评论数据不存在');
        }
    }

    //用户关注人的feed流处理
    public static function feedHandle($data)
    {
        //用户的user_id
        $user_id = $data['user_id'];

        //获取用户的关注人
        $user_attention_record_obj = Db::connect('db_mongo')->name('user_attention_record');

        //被关注的人
        $user_attention_record_result = $user_attention_record_obj->where(['follower_user_id' => $user_id, 'status' => 1])->field('by_follower_user_unique')->select();

        if (!$user_attention_record_result) {
            echo '没有关注的人';
        }

        //关注人的集合
        $author_id_arr = [];

        foreach ($user_attention_record_result as $value) {
            $author_id_arr[] = $value['by_follower_user_unique'];
        }


        $condition = [];

        $condition['author_id'] = ['in', $author_id_arr];

        //获取当前最大的feed流中最大的 item_count是多少
        $item_count_result = Db::connect('db_mongo')
            ->name('user_feed')
            ->where(['user_id' => $user_id])
            ->order(['item_count' => -1])
            ->field('item_count')
            ->find();

        if ($item_count_result) {
            $condition['item_count'] = ['>', $item_count_result['item_count']];
        }

        self::insertFeedData($condition, $user_id);
    }

    //关注的处理
    public static function attentionFeedHandle($data)
    {
        echo '关注处理';

        //关注人的user_id
        $user_id = $data['follower_user_id'];
        //被关注人的唯一标识author_id
        $author_id = $data['by_follower_user_unique'];

        //筛选条件
        $condition = [];

        $condition['author_id'] = $author_id;

        self::insertFeedData($condition, $user_id);
    }

    //取消关注的处理
    public static function cancleAttentionFeedHandel($data)
    {
        echo '取消关注的处理';
        //关注人的user_id
        $user_id = $data['follower_user_id'];
        //被关注人的唯一标识author_id
        $author_id = $data['by_follower_user_unique'];

        $author_id = '99f3f835256ff6bc5ae8a8544cebe44e';

        //删除所有的对应的feed流中的文章
        $result = Db::connect('db_mongo')
            ->name('user_feed')
            ->where(['user_id' => $user_id, 'author_id' => $author_id])
            ->delete();
    }

    //插入数据到feed中的处理
    private static function insertFeedData($condition, $user_id)
    {
        $list = Db::connect('db_mongo')
            ->name('item')
            ->where($condition)
            ->order(['item_count' => 1])
            ->field('item_count,classify_type,author_id')->select();


        $obj = Db::connect('db_mongo')->name('user_feed');

        foreach ($list as $value) {
            $value['user_id'] = $user_id;
            $value['item_id'] = $value['id'];
            unset($value['id']);
            $result = $obj->insertGetId($value);
            if ($result) {
                echo '插入一条数据成功';
            } else {
                echo '插入一条数据失败';
            }
        }
    }

    //收藏的异步处理
    public static function collectHandle(){

    }

}